﻿/*
This file is part of Ingo Karstein's SharePoint 2010 Workflow Demo project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://spworkflowdemo.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    SharePoint 2010 Workflow Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Security;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Security.Permissions;

namespace ik.SharePoint2010.Workflow
{
    public class Workflow2DataPages : LayoutsPageBase
    {
        public enum FormType
        {
            Association,
            Initiation
        }

        protected SPList list;
        protected SPContentType contentType;
        protected string workflowName;
        protected HyperLink hlReturn;
        protected string requestQueryString;
        protected bool useContentTypeTemplate = false;

        protected TextBox textboxData1;
        protected TextBox textboxData2;
        protected TextBox textboxData3;
        protected TextBox textboxData4;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            EnsureRequestParamsParsed();
            SPBasePermissions perms = SPBasePermissions.Open | SPBasePermissions.ViewPages;
            if( useContentTypeTemplate )
                perms |= SPBasePermissions.AddAndCustomizePages;
            else
                perms |= SPBasePermissions.ManageLists;
            Web.CheckPermissions(perms);
        }

        protected void EnsureRequestParamsParsed()
        {
            workflowName = Request.Params["WorkflowName"];
            string strListID = Request.QueryString["List"];
            string strCTID = Request.QueryString["ctype"];
            if( strListID != null )
                list = Web.Lists[new Guid(strListID)];
            if( strCTID != null )
            {
                requestQueryString = "ctype=" + strCTID;
                if( list != null )
                {
                    requestQueryString += "&List=" + strListID;
                    contentType = list.ContentTypes[new SPContentTypeId(strCTID)];
                }
                else
                {
                    contentType = Web.ContentTypes[new SPContentTypeId(strCTID)];
                    useContentTypeTemplate = true;
                }
            }
            else
                requestQueryString = "List=" + strListID;
        }

        internal void PopulatePageFromXml(string associationXml, FormType type)
        {
            Workflow2Data Workflow2Data = new Workflow.Workflow2Data();
            if( !string.IsNullOrEmpty(associationXml) )
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Workflow2Data));
                XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(associationXml));
                Workflow2Data = (Workflow2Data)serializer.Deserialize(reader);
            }      /* ikarstein: Start your modifications here */

            if( type == FormType.Association )
            {
                textboxData1.Text = Workflow2Data.Data1;
                textboxData2.Text = Workflow2Data.Data2;
            }
            textboxData3.Text = Workflow2Data.Data3;
            textboxData4.Text = Workflow2Data.Data4;     /* ikarstein: End of Modifications */
        }

        internal string SerializePageToXml(FormType type)
        {
            Workflow2Data data = new Workflow2Data();     /* ikarstein: Start your modifications here */
            if( type == FormType.Association )
            {
                data.Data1 = textboxData1.Text;
                data.Data2 = textboxData2.Text;
            }
            data.Data3 = textboxData3.Text;
            data.Data4 = textboxData4.Text;     /* ikarstein: End of Modifications */
            using( MemoryStream stream = new MemoryStream() )
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Workflow2Data));
                serializer.Serialize(stream, data);
                stream.Position = 0;
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                return Encoding.UTF8.GetString(bytes);
            }
        }

        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string customMasterUrl = SPControl.GetContextWeb(this.Context).MasterUrl; //~masterurl/default.master    
            this.MasterPageFile = customMasterUrl;
        }
    }
}
